package com.gangan.controller.web;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gangan.controller.common.BaseController;
import com.gangan.model.Doctor;
import com.gangan.model.Section;
import com.gangan.service.IDoctorService;
import com.gangan.service.ISectionService;
import com.gangan.tools.MessageBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 科室模块控制层
 */
@Api(description = "*科室模块控制层")
@RestController
@RequestMapping("/section")
public class SectionController extends BaseController {

    @Autowired
    ISectionService sectionService;
    @Autowired
    IDoctorService doctorService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ApiOperation(value="科室列表", notes="根据相关条件查询列表数据")
    public MessageBean list(Page page){
        return resultSuccess(sectionService.page(page));
    }

    @RequestMapping(value = "/del",method = RequestMethod.POST)
    @ApiOperation(value="移除科室", notes="根据自增主键移除科室")
    public MessageBean del(Integer id){
        if(CollectionUtils.isNotEmpty(doctorService.list(Wrappers.<Doctor>query().lambda().eq(Doctor::getSectionId,id)))){
            return resultFailed("该科室下有医生归属，不可删除");
        }
        return resultSuccess(sectionService.removeById(id));
    }
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value="新增科室", notes="将提交的科室资料保存到数据库")
    public MessageBean add(Section add){
        return resultSuccess(sectionService.save(add));
    }

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    @ApiOperation(value="编辑科室", notes="根据主键修改科室")
    public MessageBean edit(Section edit){
        return resultSuccess(sectionService.updateById(edit));
    }

}
